﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Reflection;

namespace Financial
{
    class QuoteTicker
    {
        private string[] columnNames = {"Ask", "Average Daily Volume", "Bid", "Ask (Real Time)", "Bid (Real Time)",
                                   "Book Value", "Change And Percent Change", "Change",
                                   "Change (Real Time)", "Dividend Share", "Earnings Share", "EPS Estimate Current Year",
                                   "EPS Estimate Next Year", "EPS Estimate Next Quarter", "Days Low", "Days High",
                                   "52 Week Low", "52 Week High", "52 Week Range", "Open", "Market Capitalization",
                                   "Days Range", "Previous Close", "Stock Exchange", "Volume", "Name", "PE Ratio", "Symbol"};
        public QuoteTicker() { }

        public ArrayList GetQuote(string symbol)
        {
            Quote quote = new Quote();
            ArrayList quoteList = new ArrayList();
            try
            {
                // Use Yahoo finance service to download stock data from Yahoo
                string yahooURL = @"http://download.finance.yahoo.com/d/quotes.csv?s=" + symbol + "&f=aa2bb2b3b4cc1c6dee7e8e9ghjkwoj1mpxvnrs";
                string[] symbols = symbol.Split('+');

                // Initialize a new WebRequest.
                HttpWebRequest webreq = (HttpWebRequest)WebRequest.Create(yahooURL);
                HttpWebResponse webresp = (HttpWebResponse)webreq.GetResponse();
                StreamReader strm = new StreamReader(webresp.GetResponseStream(), Encoding.ASCII);

                string content = "";            
                for (int i = 0; i < symbols.Length; i++)
                {
                    content = strm.ReadLine().Replace("\"", "");
                    string[] contents = content.ToString().Split(',');
                
                    // Initialize a Quote and add it to a list.
                    quoteList.Add(ContructQuote(contents));                            
                }
                strm.Close();
            }
            catch {}
            return quoteList;
        }

        public Quote ContructQuote(string[] contents)
        {
            Quote quote = new Quote();

            quote.Ask = contents[0];
            quote.Average_Daily_Volume = contents[1];
            quote.Bid = contents[2];
            quote.Ask_Real_time = contents[3];
            quote.Bid_Real_time = contents[4];
            quote.Book_Value = contents[5];
            quote.Change_And_Percent_Change = contents[6];
            quote.Change = contents[7];
            quote.Change_Real_time = contents[8];
            quote.DividendShare = contents[9];
            quote.EarningsShare = contents[10];
            quote.EPS_Estimate_Current_Year = contents[11];
            quote.EPS_Estimate_Next_Year = contents[12];
            quote.EPS_Estimate_Next_Quarter = contents[13];
            quote.Days_Low = contents[14];
            quote.Days_High = contents[15];
            quote.Fifty_Two_Week_Low = contents[16];
            quote.Fifty_Two_Week_High = contents[17];
            quote.Fifty_Two_Week_Range = contents[18];
            quote.Open = contents[19];
            quote.Market_Capitalization = contents[20];
            quote.Days_Range = contents[21];
            quote.Previous_Close = contents[22];
            quote.Stock_Exchange = contents[23];
            quote.Volume = contents[24];
            quote.Name = contents[25];
            quote.PE_Ratio = contents[26];
            quote.Symbol = contents[27];

            return quote;
        }

        public string[] GetColumnNames()
        {
            return columnNames;
        }
    }
}
